<?php

require_once 'business/AccountManager.php';
require_once 'const/RequestConst.php';

class AdminController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->view->admin = true;
        Business_AccountManager::requireLogin($this->getResponse(), ADMIN_LOGIN_URL, "/admin/index");
    }

    public function loginAction() {
        $this->view->admin = true;
        $request = $this->getRequest();
        $returnURL = $request->getParam(P_RETURNURL);
        $redirectURL = ADMIN_VERIFY_URL . "?" . P_RETURNURL . "=" . urlencode($returnURL);
        $this->view->redirectURL = $redirectURL;
    }

    public function verifyAction() {
        $this->view->admin = true;
        $request = $this->getRequest();
        $response = $this->getResponse();
        $email = $request->getParam(P_EMAIL);
        $password = $request->getParam(P_PASSWORD);
        $result = Business_AccountManager::verify($email, $password, true);
        if ($result == true) {
            $returnURL = $request->getParam(P_RETURNURL);
            if (strlen($returnURL) == 0) {
                $response->setRedirect("/admin/index");
            } else {
                $response->setRedirect($returnURL);
            }
        }
    }

    public function logoutAction() {
        Business_AccountManager::logout($this->getResponse(), "/");
    }

    public function productsAction() {
        $this->view->admin = true;
        $types = Application_Model_TypeMapper::fetchAll();
        $typeID = $this->getRequest()->getParam("type");
        $products = "";
        if (strlen($typeID) != 0) {
            $products = Application_Model_ProductMapper::fetchByType($typeID);
        } else {
            $products = Application_Model_ProductMapper::fetchAll();
        }
        $this->view->types = $types;
        $this->view->products = $products;
    }

    public function typeAction() {
        $this->view->admin = true;
        $request = $this->getRequest();
        $cmd = $request->getParam("cmd");
        if ($cmd == "delete") {
            $id = $request->getParam("id");
            Application_Model_TypeMapper::delete($id);
        } else if ($cmd == "modify") {
            $id = $request->getParam("id");
            $this->view->id = $id;
            $modifyType = Application_Model_TypeMapper::fetch($id);
            $this->view->modifyName = $modifyType[C_NAME];
        }
        $result = Application_Model_TypeMapper::fetchAll();
        $this->view->types = $result;
    }

    public function addTypeAction() {
        $this->view->admin = true;
        $request = $this->getRequest();
        $typeName = $request->getParam(P_TYPE_NAME);
        $id = $request->getParam("id");
        $type = new Application_Model_Type($typeName);
        if (strlen($id) != 0) {
            $type->setId($id);
            Application_Model_TypeMapper::update($type);
        } else {
            Application_Model_TypeMapper::insert($type);
        }
        $this->getResponse()->setRedirect("/admin/type");
    }

    public function modifyTypeAction() {
        // action body
    }

    public function addProductAction() {
        $this->view->admin = true;
        $request = $this->getRequest();
        if ($request->isGet()) {
            $id = $request->getParam("id");
            $cmd = $request->getParam("cmd");
            if ($cmd == "delete") {
                Application_Model_ProductMapper::delete($id);
                $this->getResponse()->setRedirect("/admin/products");
            } else {
                $types = Application_Model_TypeMapper::fetchAll();
                $this->view->types = $types;
                if (strlen($id) != 0) {
                    $product = Application_Model_ProductMapper::fetch($id);
                    $this->view->id = $id;
                    $this->view->product = $product;
                }
            }
        } else {
            $name = $request->getParam(P_NAME);
            $description = $request->getParam(P_DESCRIPTION);
            $typeID = $request->getParam(P_TYPE_ID);
            $price = $request->getParam(P_PRICE);
            $num = $request->getParam(P_NUM);
            $product = new Application_Model_Product($name, $description, $price, $num, $typeID);
            $id = $request->getParam("id");
            $returnURL = "";
            if (strlen($id) != 0) {
                $product->setId($id);
                Application_Model_ProductMapper::update($product);
                $returnURL = "/admin/products";
            } else {
                $id = Application_Model_ProductMapper::insert($product);
                $returnURL = "/admin/add-product";
            }
            $upload = new Zend_File_Transfer_Adapter_Http();
            $upload->setDestination(APPLICATION_PATH . "/../public/upload");
            $upload->addValidator('Extension', false, 'gif,jpeg,png,jpg');
            $upload->addValidator('Count', false, array('min' => 1, 'max' => 1));
            $upload->addFilter('Rename', array('target' => $id, 'overwrite' => true));
            $upload->receive();
            //
            $response = $this->getResponse();
            $response->setRedirect($returnURL);
        }
    }

    public function ordersAction() {
        $this->view->admin = true;
        $this->view->orders = Application_Model_OrderMapper::fetchAll();
    }

}

